﻿(function () {

	function tableborderCommands(editor, border) {
		var cells = CKEDITOR.plugins.tabletools.getSelectedCells(editor.getSelection());
		for (var i = 0; i < cells.length; i++) {
			var cell = cells[i];
			if (border) {
				cell.setStyle(border, "1px solid #000000");
			} else {
				var width = cell.getStyle("width");
				cell.$.removeAttribute('style');
				if (width != "") {
					cell.setStyle("width", width);
				}
			}
		}
	}
	CKEDITOR.plugins.add('tableborder',
	{
	    requires: ['table', 'tabletools', 'menubutton'],
		init: function (editor) {
			var menuGroup = 'tableborderButton';
			editor.addMenuGroup(menuGroup);
			editor.addCommand('tableborderAll', {
				exec: function (editor) {
					tableborderCommands(editor, "border");
				}
			});
			editor.addCommand('tableborderTop', {
				exec: function (editor) {
					tableborderCommands(editor, "border-top");
				}
			});
			editor.addCommand('tableborderBottom', {
				exec: function (editor) {
					tableborderCommands(editor, "border-bottom");
				}
			});
			editor.addCommand('tableborderLeft', {
				exec: function (editor) {
					tableborderCommands(editor, "border-left");
				}
			});
			editor.addCommand('tableborderRight', {
				exec: function (editor) {
					tableborderCommands(editor, "border-right");
				}
			});
			editor.addCommand('tableborderClear', {
				exec: function (editor) {
					tableborderCommands(editor, "");
				}
			});
			editor.addMenuItems({
				tableborderAll: {
					label: "全边框",
					command: "tableborderAll",
					icon: this.path + 'borderall.png',
					group: menuGroup
				},
				tableborderTop: {
					label: "上边框",
					command: "tableborderTop",
					icon: this.path + 'bordertop.png',
					group: menuGroup
				},
				tableborderBottom: {
					label: "下边框",
					command: "tableborderBottom",
					icon: this.path + 'borderbottom.png',
					group: menuGroup
				},
				tableborderLeft: {
					label: "左边框",
					command: "tableborderLeft",
					icon: this.path + 'borderleft.png',
					group: menuGroup
				},
				tableborderRight: {
					label: "右边框",
					command: "tableborderRight",
					icon: this.path + 'borderright.png',
					group: menuGroup
				},
				tableborderClear: {
					label: "无边框",
					command: "tableborderClear",
					icon: this.path + 'borderclear.png',
					group: menuGroup
				}
			});
			editor.ui.add('TableBorder', CKEDITOR.UI_MENUBUTTON, {
				label: '表格边框',
				icon: this.path + 'borderall.png',
				modes: { wysiwyg: 1 },
				onMenu: function (element, selection) {
					var selectedTag = element.getName().toLowerCase();
					var state = CKEDITOR.TRISTATE_DISABLED;
					if (selectedTag == 'td' || selectedTag == "th") {
						state = CKEDITOR.TRISTATE_OFF;
					}
					return {
						tableborderAll: state,
						tableborderTop: state,
						tableborderBottom: state,
						tableborderLeft: state,
						tableborderRight: state,
						tableborderClear: state
					};
				}
			});
			return;
			editor.on('selectionChange', function (ev) {
				var selectedTag = ev.data.element.getName().toLowerCase();
				if (selectedTag == 'td' || selectedTag == "th") {

				}
				var cells = CKEDITOR.plugins.tabletools.getSelectedCells(editor.getSelection());
				for (var i = 0; i < cells.length; i++) {
					var cell = cells[i];
					//cell.setStyle("border-top", "1px solid black");
				}
			});
		}
	});
})();
